package com.css.fxfzysx.modules.largeScreen.repository.impl;

import com.css.fxfzysx.constants.RelationConstants;
import com.css.fxfzysx.modules.largeScreen.constant.StructureTypeConstant;
import com.css.fxfzysx.modules.largeScreen.param.ProbabilisticEarthquakeParam;
import com.css.fxfzysx.modules.largeScreen.repository.LargeScreenRepository;
import com.css.fxfzysx.modules.largeScreen.vo.CpvAnalysisVo;
import com.css.fxfzysx.modules.largeScreen.vo.CpvResultVo;
import com.css.fxfzysx.modules.largeScreen.vo.MatrixVo;
import com.css.fxfzysx.modules.largeScreen.vo.ProbabilisticEarthquakeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LargeScreenRepositoryImpl implements LargeScreenRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<MatrixVo> getByRegion(String province,String county,String intensity) {
        String sql = "select * from "+ RelationConstants.getConstant(province)+" cbv left join czt_building_vulnerability_info_matrix_value civ on cbv.cbvi_id = civ.cbvi_id where cbv.del_flag = '0' and civ.intensity = '"+intensity+"'  and basic_library = '1' and  county = '"+county+"' and curve_or_matrix = '1' and structure_type in ("+ StructureTypeConstant.STRUCTURE_TYPE+") and structure_type1 in ("+StructureTypeConstant.STRUCTURE_TYPE1+")";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(MatrixVo.class));
    }

    @Override
    public ProbabilisticEarthquakeVo getInfo(ProbabilisticEarthquakeParam param) {
        try {
            String sql = "select cpvi_id,gmrcp_id from czt_probability_vulnerability_info where del_flag = '0' and structure_type0 = '"+param.getStructureType0()+"' and structure_type1 = '"+param.getStructureType1()+"' and province = '"+param.getProvince()+"' and city = '"+param.getCity()+"' and county = '"+param.getCounty()+"' and fortification = '"+param.getFortification()+"' and purpose0 = '"+param.getPurpose0()+"' and purpose1 = '"+param.getPurpose1()+"' and years = '"+param.getYears()+"' and cs = '"+param.getCs()+"' order by create_time desc";
            List<ProbabilisticEarthquakeVo> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProbabilisticEarthquakeVo.class));
            return query.get(0);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<CpvAnalysisVo> getCpvAnalysisEntityByCpviId(String cpviId) {
        String sql = "select * from czt_probability_vulnerability_analysis where del_flag='0' and cpvi_id='" + cpviId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CpvAnalysisVo.class));
    }

    @Override
    public List<CpvResultVo> getCpvResultEntityByCpviId(String cpviId) {
        String sql = "select * from czt_probability_vulnerability_result where del_flag='0' and cpvi_id='" + cpviId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CpvResultVo.class));
    }
}
